﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Infrastructure;

namespace CoVanHocTapData.Concretes
{
    public class MonHocRepository : IMonHocRepository
    {
        DangKyPhuEntities entities;
        public MonHocRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region Repo
        ISinhVienRepository svRepos = new SinhVienRepository();
        IChuongTrinhDaoTaoRepository ctdtRepos = new ChuongTrinhDaoTaoRepository();
        #endregion

        public IQueryable<MonHoc> LayMonHoc()
        {
            return (from m in entities.MonHocs
                    select m);
        }

        public IQueryable<MonHoc> LayMonHoc(string khoaID)
        {
            return (from m in entities.MonHocs
                    where m.Khoa.KhoaID.Equals(khoaID)
                    select m);
        }

        /// <summary>
        /// lay mh bat buoc trog ctdt
        /// </summary>
        /// <param name="monhocID"></param>
        /// <param name="hocKy"></param>
        /// <param name="nganhID"></param>
        /// <returns></returns>
        public MonHocTheoHocKy LayMonHocTheoMaMH(string monhocID, string hocKy, string nganhID)
        {
            var rangbuocList = from m in entities.RangBuocMons
                               where m.MonHocIDSau.Equals(monhocID)
                               && m.NganhID.Equals(nganhID)
                               select m;
            //0: la mon tien quyet; 1 la mon hoc truoc; 2: mh song hanh
            string tienQuyetList = "";
            string truocList = "";
            string songHanhList = "";

            foreach (var rangbuocItem in rangbuocList)
            {

                var monHocRangBuoc = (from m in entities.MonHocs
                                      where m.MonHocID.Equals(rangbuocItem.MonHocIDTruoc)
                                      select m).ToList().FirstOrDefault();
                string strMHRangBuoc = "<b>" + monHocRangBuoc.MonHocID + "</b> - " + monHocRangBuoc.TenMonHoc;
                if (rangbuocItem.LoaiRangBuoc == 0)
                {
                    if (tienQuyetList.IndexOf(strMHRangBuoc) == -1)
                        tienQuyetList += strMHRangBuoc + "<br/>";
                }
                else if (rangbuocItem.LoaiRangBuoc == 1)
                {
                    if (truocList.IndexOf(strMHRangBuoc) == -1)
                        truocList += strMHRangBuoc + "<br/>";
                }
                else if (rangbuocItem.LoaiRangBuoc == 2)
                {
                    if (songHanhList.IndexOf(strMHRangBuoc) == -1)
                        songHanhList += strMHRangBuoc + "<br/>";
                }
            }

            var result = (from m in entities.MonHocs
                          where m.MonHocID.Equals(monhocID)
                          select new MonHocTheoHocKy()
                          {
                              DVHT = m.SoDVHocTap.Value,
                              HocKy = hocKy,
                              MonHocID = m.MonHocID,
                              TenMonHoc = m.TenMonHoc,
                              MonHocSongHanh = songHanhList,
                              MonHocTienQuyet = tienQuyetList,
                              MonHocTruoc = truocList,
                              LaMonTuChon = false,
                          }).ToList().FirstOrDefault();
            return result;
        }

        public MonHoc LayMonHocTheoMaMH(string monHocID)
        {
            var mh = (from m in entities.MonHocs
                      where m.MonHocID.Equals(monHocID)
                      select m).FirstOrDefault();
            return mh;
        }

        public IQueryable<MonHoc> NhomAVGT()
        {
            return (from m in entities.MonHocs
                    where m.MonHocID.Equals("001001")
                    || m.MonHocID.Equals("001002")
                    || m.MonHocID.Equals("001003")
                    || m.MonHocID.Equals("001004")
                    || m.MonHocID.Equals("001005")
                    || m.MonHocID.Equals("001006")
                    || m.MonHocID.Equals("AV000")
                    || m.MonHocID.Equals("AV129")
                    || m.MonHocID.Equals("AV139")
                    || m.MonHocID.Equals("AV149")
                    || m.MonHocID.Equals("AV229")
                    || m.MonHocID.Equals("AV239")
                    || m.MonHocID.Equals("AV249")
                    || m.MonHocID.Equals("AV339")
                    || m.MonHocID.Equals("AV349")
                    || m.MonHocID.Equals("AV359")
                    || m.MonHocID.Equals("AV409")
                    || m.MonHocID.Equals("AV419")
                    || m.MonHocID.Equals("AV439")
                    || m.MonHocID.Equals("AV539")
                    || m.MonHocID.Equals("AV619")
                    || m.MonHocID.Equals("AV719")
                    || m.MonHocID.Equals("AV729")
                    select m);

        }

        public IQueryable<MonHoc> NhomGDQP()
        {
            return (from m in entities.MonHocs
                    where m.MonHocID.Equals("305001")
                    || m.MonHocID.Equals("305002")
                    || m.MonHocID.Equals("305003")
                    || m.MonHocID.Equals("305004")
                    || m.MonHocID.Equals("305005")
                    || m.MonHocID.Equals("QP101")
                    || m.MonHocID.Equals("QP102")
                    || m.MonHocID.Equals("QP103")
                    || m.MonHocID.Equals("QPHP1")
                    || m.MonHocID.Equals("QPHP2")
                    || m.MonHocID.Equals("QPHP3")
                    || m.MonHocID.Equals("QPHP4")
                    select m);
        }

        public IQueryable<MonHoc> NhomGDTC()
        {
            return (from m in entities.MonHocs
                    where m.MonHocID.Equals("304001")
                    || m.MonHocID.Equals("304002")
                    || m.MonHocID.Equals("304003")
                    || m.MonHocID.Equals("304013")
                    || m.MonHocID.Equals("304022")
                    || m.MonHocID.Equals("304023")
                    || m.MonHocID.Equals("F00008")
                    || m.MonHocID.Equals("F00015")
                    || m.MonHocID.Equals("F00017")
                    || m.MonHocID.Equals("TC101")
                    || m.MonHocID.Equals("TC102")
                    || m.MonHocID.Equals("TC103")
                    || m.MonHocID.Equals("TC104")
                    || m.MonHocID.Equals("TC105")
                    || m.MonHocID.Equals("TC106")
                    select m);
        }


        public List<MonHoc> LayDanhSachAVGTDaHT(string maSV)
        {
            var dsmh = NhomAVGT();
            var dsmhdaHT = new List<MonHoc>();
            foreach (var mon in dsmh)
            {
                var ht = (from m in entities.MonHocs
                          join k in entities.KetQuaHocs
                          on m.MonHocID equals k.MonHocID
                          join ct in entities.ChuongTrinhDaoTaos
                          on m.MonHocID equals ct.MonHocID
                          where k.MSSV == maSV
                          && m.MonHocID == mon.MonHocID
                          && k.DTB >= 5
                          select m).FirstOrDefault();
                if (ht != null)
                    dsmhdaHT.Add(ht);
            }
            return dsmhdaHT;
        }

        public List<MonHoc> LayDanhSachAVGTChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }

        public List<MonHoc> LayDanhSachGDTCDaHT(string maSV)
        {
            var dsmh = NhomGDTC();
            var dsmhdaHT = new List<MonHoc>();
            foreach (var mon in dsmh)
            {
                var avHT = (from m in entities.MonHocs
                            join k in entities.KetQuaHocs
                            on m.MonHocID equals k.MonHocID
                            join ct in entities.ChuongTrinhDaoTaos
                            on m.MonHocID equals ct.MonHocID
                            where k.MSSV == maSV
                            && m.MonHocID == mon.MonHocID
                            && k.DTB >= 5
                            select m).FirstOrDefault();
                if (avHT != null)
                    dsmhdaHT.Add(avHT);
            }
            return dsmhdaHT;

        }

        public List<MonHoc> LayDanhSachGDTCChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }

        public List<MonHoc> LayDanhSachGDQPDaHT(string maSV)
        {
            var dsmh = NhomGDQP();
            var dsmhdaHT = new List<MonHoc>();
            foreach (var mon in dsmh)
            {
                var avHT = (from m in entities.MonHocs
                            join k in entities.KetQuaHocs
                            on m.MonHocID equals k.MonHocID
                            join ct in entities.ChuongTrinhDaoTaos
                            on m.MonHocID equals ct.MonHocID
                            where k.MSSV == maSV
                            && m.MonHocID == mon.MonHocID
                            && k.DTB >= 5
                            select m).FirstOrDefault();
                if (avHT != null)
                    dsmhdaHT.Add(avHT);
            }
            return dsmhdaHT;
        }

        public List<MonHoc> LayDanhSachGDQPChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }

        public List<MonHoc> LayDSMHTuChuoiMaMH(string chuoiMaMH)
        {
            var dsMH = new List<MonHoc>();
            string[] mangMaMH = chuoiMaMH.Split(',');
            var monhoc = new MonHoc();

            string[] mhTuongDuong;

            foreach (var maMH in mangMaMH)
            {
                string t_maMH = maMH.Trim();                
                if (maMH.Contains('-'))
                {
                    mhTuongDuong = t_maMH.Split('-');
                    monhoc = LayMonHocTheoMaMH(mhTuongDuong[0]);
                    if (monhoc != null)
                    {
                        monhoc.maMonHocTam = monhoc.MonHocID;
                        dsMH.Add(monhoc);
                    }
                    monhoc = LayMonHocTheoMaMH(mhTuongDuong[1]);
                    if (monhoc != null)
                    {
                        if (monhoc.TenMonHoc.Contains(')'))
                            monhoc.TenMonHoc = monhoc.TenMonHoc.Remove(monhoc.TenMonHoc.Length-1);
                        monhoc.maMonHocTam = "(" + monhoc.MonHocID;
                        monhoc.TenMonHoc = monhoc.TenMonHoc + ")";
                        dsMH.Add(monhoc);
                    }
                }
                else
                {
                    monhoc = LayMonHocTheoMaMH(t_maMH);
                    if (monhoc != null)
                    {
                        monhoc.maMonHocTam = monhoc.MonHocID;
                        dsMH.Add(monhoc);
                    }
                }

            }
            return dsMH;
        }



        public MonHoc MonHocDGRL()
        {
            var mh = (from m in entities.MonHocs
                      where m.MonHocID == "DGRL"
                      select m).FirstOrDefault();
            return mh;
        }
    }
}
